﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Normal_TagsController : Controller
    {

        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回标签列表
        /// </summary>
        /// <returns>json</returns>
        public string List()
        {
            //返回json数组

            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
            List<Normal_TagsEntity> normal_tagsList = Normal_TagsSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_tagsList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;

        }

        /// <summary>
        /// 返回标签列表
        /// </summary>
        /// <returns>json</returns>
        public string List2()
        {
            //返回json数组

            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            string query = (string)Utils.GetQueryAndFormString("query", this, 9, 1, DataType.Str);
            QueryParam qp = new QueryParam();
            qp.PageIndex = pageIndex / pageSize + 1; ;
            qp.PageSize = pageSize;
            qp.Where = string.Concat("tagName like'%", query, "%'");
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
            List<Normal_TagsEntity> normal_tagsList = Normal_TagsSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(normal_tagsList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;

        }

        /// <summary>
        /// 查看标签 
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
            IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
            int intId = Utils.StringToInt(actionId, 0);
            Normal_TagsEntity normal_tags = Normal_TagsSv.Look(intId);
            if (normal_tags != null)
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(normal_tags), "}");
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新标签
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        [ValidateInput(false)]
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_TagsEntity normal_tags = new Normal_TagsEntity();
                normal_tags.DataTable_Action = DataTableAction.Update;
                normal_tags.Id = intId;
                normal_tags.TagName = (String)Utils.GetQueryAndFormString("TagName", this, 50, 1, DataType.Str);
                normal_tags.HtmlInfo = (String)Utils.GetQueryAndFormString("HtmlInfo", this, 8000, 0, DataType.Str);
                normal_tags.SeoTagName = (String)Utils.GetQueryAndFormString("SeoTagName", this, 300, 0, DataType.Str);
                normal_tags.Dir = (String)Utils.GetQueryAndFormString("Dir", this, 300, 0, DataType.Str);
                normal_tags.ChannelPath = (String)Utils.GetQueryAndFormString("ChannelPath", this, 14, 0, DataType.Str);
                normal_tags.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10, 0, DataType.Int);
                normal_tags.IsRecommend = (Int32)Utils.GetQueryAndFormString("IsRecommend", this, 1, 0, DataType.Int);
                IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
                if (Normal_TagsSv.Update(normal_tags) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }

            return returnJson;
        }

        /// <summary>
        /// 新增标签
        /// </summary>
        /// <returns>json</returns>
        [ValidateInput(false)]
        public string Insert()
        {
            Normal_TagsEntity normal_tags = new Normal_TagsEntity();
            normal_tags.DataTable_Action = DataTableAction.Insert;
            normal_tags.TagName = (String)Utils.GetQueryAndFormString("TagName", this, 50, 1, DataType.Str);
            normal_tags.HtmlInfo = (String)Utils.GetQueryAndFormString("HtmlInfo", this, 8000, 0, DataType.Str);
            normal_tags.ChannelPath = (String)Utils.GetQueryAndFormString("ChannelPath", this, 14, 0, DataType.Str);
            normal_tags.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10, 0, DataType.Int);
            normal_tags.IsRecommend = (Int32)Utils.GetQueryAndFormString("IsRecommend", this, 1, 0, DataType.Int);
            normal_tags.SeoTagName = (String)Utils.GetQueryAndFormString("SeoTagName", this, 300, 0, DataType.Str);
            normal_tags.Dir = (String)Utils.GetQueryAndFormString("Dir", this, 300, 0, DataType.Str);
            if (string.IsNullOrEmpty(normal_tags.Dir))
            {
                normal_tags.Dir = Utils.ChineseToQuanPin(normal_tags.TagName);
            }
            IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
            string strRetrun = Normal_TagsSv.Insert(normal_tags);
            if (strRetrun != "0")
            {
                returnJson = "{success:'true',message:" + strRetrun + "}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="actionId"></param>
        /// <returns>json</returns>
        public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Normal_TagsEntity normal_tags = new Normal_TagsEntity();
                normal_tags.Id = intId;
                IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
                if (Normal_TagsSv.Delete(normal_tags) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除标签
        /// </summary>
        /// <param name="actionIdList">actionId列表 234_4_5</param>
        /// <returns>json</returns>
        public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Normal_TagsEntity> Normal_TagsSv = ServiceBuilder.BuildNormal_TagsService();
                if (Normal_TagsSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
